// pages/CPSincome/CPSincome.js
const app = getApp();
import api from '../../../../utils/util.js';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: app.globalData.statusBarHeight,
    fullHeight: app.globalData.fullcontain,
    navmess: {
      navtitle: "我的CPS收入",
      backhide: 0,
      topheight: 1
    },
    date: {
      "year": "年",
      "month": "月"
    },
    // 消费累计记录
    records: {
      "withDrawal": "1800.00",
      "allIncome": "40000.00",
      "grandDrawal": "40000.00",
      "grandConsume": "40000.00"
    },
    // ajaxdemo
    billlist: {
      "pages": 2,
      "page1":[
          {
          "month": "1月",
          "addvipNum": "12",
          "consumeNum": "5",
          "extensionNum": "132",
          "recharge": "231321.11",
          "probuy": "231321.11",
          "proconsume": "231321.11",
          "allmon": "3838.38"
        },
        {
          "month": "2月",
          "addvipNum": "12",
          "consumeNum": "5",
          "extensionNum": "132",
          "recharge": "231321.11",
          "probuy": "231321.11",
          "proconsume": "231321.11",
          "allmon": "3838.38"
        }
      ],
      "page2":[
        {
          "month": "3月",
          "addvipNum": "12",
          "consumeNum": "5",
          "extensionNum": "132",
          "recharge": "231321.11",
          "probuy": "231321.11",
          "proconsume": "231321.11",
          "allmon": "3838.38"
        },
        {
          "month": "4月",
          "addvipNum": "12",
          "consumeNum": "5",
          "extensionNum": "132",
          "recharge": "231321.11",
          "probuy": "231321.11",
          "proconsume": "231321.11",
          "allmon": "3838.38"
        },
        {
          "month": "5月",
          "addvipNum": "12",
          "consumeNum": "5",
          "extensionNum": "132",
          "recharge": "231321.11",
          "probuy": "231321.11",
          "proconsume": "231321.11",
          "allmon": "3838.38"
        }
      ]
    },
    renderList: [],
    pageNum: 1, //当前页码
    pageSize: 3, 
    pages: 0, // 总页码
    loading: false, 
    noMore: false,
  },
  // 日期picker
  bindDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value);
    var arr = e.detail.value.split('-');
    this.setData({
      date: {
        "year": arr[0],
        "month": arr[1]
      }
    })
  },
  // 初始化数据请求
  loadData: function(){
    this.setData({
      pageNum: 1
    })
    this.getData(false); //请求数据
  },
  //到达底部
  scrollToLower: function (e) {
    if (!this.data.loading && !this.data.noMore){
      this.setData({
        loading: true,
        pageNum: this.data.pageNum + 1
      })
      this.getData(true);
    }
  },
  //请求数据
  getData(isPage) {
    console.log("需要拼接数据：", this.data.renderList);
    let params = {
      pageNum: this.data.pageNum,
      pageSize: this.data.pageSize
    }
    //请求
    api.post("",params).then(res =>{
      this.setData({
        loading: false
      })
      if (res.code == 200) {
        if (isPage) {
          //下一页的数据拼接在原有数据后面
          this.setData({
            renderList: this.data.renderList.concat(res.result.list)
          })
        } else {
          //第一页数据直接赋值
          this.setData({
            renderList: res.result.list
          })
        }
        //如果当前为尾页
        if (res.result.isLastPage) {
          this.setData({
            noMore: true
          })
        }
      }
    }).catch(res => {
      //返回请求失败信息
      this.setData({
        loadingFailed: true
      })
      return false;
    })
  },
  // ******模拟分页处理******
  /**
   * 页面触底事件的处理函数
   */
  scrollToLower: function() {
    console.log("进入触底函数")
    var that = this;
    if (!this.loading && this.data.pageNum < this.data.pages) {
      this.getMoreList();
    }else{
      console.log("--------")
      wx.showToast({
        title: '我也是有底线的~',
        icon:'none',
        duration:2500,
      })
    }
  },
  getMoreList: function(){
    this.loading = true
    console.log("请求分页")
    var that =this;
    (this.data.pageNum)++;
    this.setData({
      renderList: that.data.renderList.concat(that.data.billlist.page2)
    })
    this.loading = false
  },

  // top下拉刷新 
  //触摸开始
   handletouchstart: function (event) {
    this.refreshView.handletouchstart(event)
  },
  //触摸移动
  handletouchmove: function (event) {
    this.refreshView.handletouchmove(event)
  },
  //触摸结束
  handletouchend: function (event) {
    this.refreshView.handletouchend(event)
  },
  //触摸取消
  handletouchcancel: function (event) {
    this.refreshView.handletouchcancel(event)
  },
  //页面滚动
  onPageScroll: function (event) {
    this.refreshView.onPageScroll(event)
  },
  onPullDownRefresh:function(){
    console.log("->刷新，请求第一页数据")
    // this.loadData();
    setTimeout(() => { this.refreshView.stopPullRefresh()},2000)
  },
  _pullState: function(e){
    // console.log(e.detail) 
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // *******分页测试数据********
    this.setData({
      renderList: this.data.billlist.page1,
      pages: this.data.billlist.pages
    })
    // 刷新组件
    this.refreshView = this.selectComponent("#refreshView")
    // this.loadData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})